package com.xpsoft.oa.workflow.event;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.produce.MachineRecord;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.produce.MachineRecordService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;

public class MachineRecordFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(MachineRecordFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proId = (String)pi.getVariable("machineRecord.project.id");
		Date workDate = (Date)pi.getVariable("machineRecord.workDate");
		String countNo = (String)pi.getVariable("machineRecord.countNo");
		String machineName = (String)pi.getVariable("machineRecord.machineName");
		String model = (String)pi.getVariable("machineRecord.model");
		String carNo = (String)pi.getVariable("machineRecord.carNo");
		String operatorName = (String)pi.getVariable("machineRecord.operatorName");
		String operatorId = (String)pi.getVariable("machineRecord.operatorId");
		String operatorTel = (String)pi.getVariable("machineRecord.operatorTel");
		String startAm = (String)pi.getVariable("machineRecord.startAm");
		String endAm = (String)pi.getVariable("machineRecord.endAm");
		String startPm = (String)pi.getVariable("machineRecord.startPm");
		String endPm = (String)pi.getVariable("machineRecord.endPm");
		String startNight = (String)pi.getVariable("machineRecord.startNight");
		String endNight = (String)pi.getVariable("machineRecord.endNight");
		String workHours = (String)pi.getVariable("machineRecord.workHours");
		String workContent = (String)pi.getVariable("machineRecord.workContent");
		String applyUserId = (String)pi.getVariable("machineRecord.applyUser.userId");
		
		MachineRecord machineRecord = new MachineRecord();
		machineRecord.setWorkDate(workDate);
		machineRecord.setCountNo(countNo);
		machineRecord.setMachineName(machineName);
		machineRecord.setModel(model);
		machineRecord.setCarNo(carNo);
		machineRecord.setOperatorName(operatorName);
		if(StringUtils.isNotBlank(operatorId)){
			machineRecord.setOperatorId(Long.valueOf(operatorId));
		}
		machineRecord.setOperatorTel(operatorTel);
		machineRecord.setStartAm(startAm);
		machineRecord.setEndAm(endAm);
		machineRecord.setStartPm(startPm);
		machineRecord.setEndPm(endPm);
		machineRecord.setStartNight(startNight);
		machineRecord.setEndNight(endNight);
		machineRecord.setWorkHours(workHours);
		machineRecord.setWorkContent(workContent);
		
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		if(StringUtils.isNotBlank(applyUserId)){
			machineRecord.setApplyUser(appUserService.get(Long.valueOf(applyUserId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			machineRecord.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		machineRecord.setProcessRunId(processRun.getRunId());
		
		MachineRecordService machineRecordService = (MachineRecordService)AppUtil.getBean("machineRecordService");
		machineRecordService.save(machineRecord);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
